import Constant from './ui-constant';

const whiteChars = {"\t": "\u0009", "\n": "\u000A", "\r": "\u000D", " ": "\u0020"};
const _collapseLeftInlines=(inlines, targetInline)=> {
    if (!targetInline) return;
    for (let i = 0; i < inlines.length; i++) {
        const inline = inlines[i];
        inline.width = 0;
        inline.height = 0;
        inline.offsetX = targetInline.offsetX;
    }
};
const _collapseRightInlines=(inlines, targetInline)=> {
    if (!targetInline) return;
    for (let i = 0; i < inlines.length; i++) {
        const inline = inlines[i];
        inline.width = 0;
        inline.height = 0;
        inline.offsetX = targetInline.offsetX + targetInline.width;
    }
};

export default class UISpace {

    static collapseContent(textContent, whiteSpace) {
        switch (whiteSpace) {
            case Constant.VALUE_NORMAL:
                textContent = textContent.replace(/\n/g, " ");
            case Constant.VALUE_PRE_LINE:
                textContent = textContent.replace(/[ ]{2,}/g, " ");
                break;

            default:
        }

        return textContent;
    }
    static newlineBreakability(whiteSpace) {
        switch (whiteSpace) {
            case Constant.VALUE_PRE_WRAP:
            case Constant.VALUE_PRE_LINE:
                return Constant.VALUE_MANDATORY;
            case Constant.VALUE_NORMAL:
            default:
        }

    }
    static collapseInlines(line, whiteSpace) {
        let firstInline = line[0];
        let lastInline = line[line.length - 1];
        switch (whiteSpace) {
            case Constant.VALUE_PRE_WRAP:
                if (firstInline.glyph && firstInline.glyph === "\n" && line.length > 1) {
                    _collapseLeftInlines([firstInline], line[1])
                }
                if (lastInline.glyph && lastInline.glyph === "\n" && line.length > 1) {
                    _collapseRightInlines([lastInline], line[line.length - 2])
                }
                break;
            case Constant.VALUE_PRE_LINE:
            case Constant.VALUE_NORMAL:
                let inlinesToCollapse = [];
                let collapsingTarget;
                for (let i = 0; i < line.length; i++) {
                    const inline = line[i];
                    if (inline.glyph && whiteChars[inline.glyph] && line.length > i) {
                        inlinesToCollapse.push(inline);
                        collapsingTarget = line[i + 1];
                        continue;
                    }
                    break;
                }
                _collapseLeftInlines(inlinesToCollapse, collapsingTarget);
                inlinesToCollapse = [];
                collapsingTarget = null;
                for (let i = line.length - 1; i > 0; i--) {
                    const inline = line[i];
                    if (inline.glyph && whiteChars[inline.glyph] && i > 0) {
                        inlinesToCollapse.push(inline);
                        collapsingTarget = line[i - 1];
                        continue;
                    }
                    break;
                }
                _collapseRightInlines(inlinesToCollapse, collapsingTarget);
                break;
            default:
                console.warn(`whiteSpace: '${whiteSpace}' is not valid`);
                return 0;
        }
        return firstInline.offsetX;
    }

}